package com.gameShare.service.user

import com.gameShare.entity.dto.user.UserFocusNumberDto
import com.gameShare.entity.vo.FocusUserVo
import com.gameShare.entity.vo.PageVo
import com.gameShare.enums.misc.ResponseCodeEnum
import com.gameShare.enums.user.UserFocusSelectType
import com.gameShare.exceptions.BusinessException
import com.gameShare.mappers.user.UserFocusMapper
import org.springframework.stereotype.Service

@Service
class UserFocusService(private val userFocusMapper: UserFocusMapper) {

    /**
     *
     *
     * @param pageNo 页码
     * @param pageSize 页面大小
     * @param userId 用户id
     * @param selectType 查询类型
     * @return
     */
    fun loadUserFocusPage(pageNo : Int?, pageSize : Int?, userId : String, selectType : Int): PageVo<FocusUserVo?> {
        return when (selectType) {
            UserFocusSelectType.FOCUS.code -> PageVo.generatePageVo(
                pageNo,
                pageSize,
                {userFocusMapper.selectUserFocusCount(userId)},
                {start, end -> userFocusMapper.selectUserFocus(userId, start, end)}
            )
            UserFocusSelectType.BE_FOCUS.code -> PageVo.generatePageVo(
                pageNo,
                pageSize,
                {userFocusMapper.selectBeUserFocusCount(userId)},
                {start, end -> userFocusMapper.selectBeUserFocus(userId, start, end)}
            )
            else -> throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
        }
    }

    fun loadUserFocusNumber(userId : String): UserFocusNumberDto {
        return UserFocusNumberDto(
            userFocusMapper.selectUserFocusCount(userId),
            userFocusMapper.selectBeUserFocusCount(userId)
        )
    }

    /**
     * 关注
     *
     * @param userId
     * @param focusUserId
     * @return
     */
    fun focus(userId : String, focusUserId : String): Boolean {
        if (userId == focusUserId)
            throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR.code, "用户不能关注自己")
        if (userFocusMapper.checkFocusExist(userId, focusUserId) == 1){
            throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR.code, "用户已经关注此人")
        }
        return userFocusMapper.insert(userId, focusUserId) == 1
    }

    /**
     * 取消关注
     *
     * @param userId
     * @param focusUserId
     * @return
     */
    fun cancelFocus(userId : String, focusUserId : String): Boolean {
        if (userId == focusUserId)
            throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR.code, "用户不能取消关注自己")
        return userFocusMapper.delete(userId, focusUserId) == 1
    }

    /**
     * 加载用户的关注和被关注数量
     *
     * @param userId 用户id
     * @param selectType 选择类型
     * @return
     */
    fun loadFocusCount(userId : String, selectType: Int) : Int{
        return when(selectType){
            UserFocusSelectType.FOCUS.code -> userFocusMapper.selectUserFocusCount(userId)
            UserFocusSelectType.BE_FOCUS.code -> userFocusMapper.selectBeUserFocusCount(userId)
            else -> throw BusinessException(ResponseCodeEnum.ARGUMENT_ERROR)
        }
    }
}